package com.mask.lego.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mask.lego.order.vo.PayVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author hx
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
@Slf4j
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
//    public String app_id = "2021000117662885";
    public String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
//    public String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCU5LJjQrXLUy0myCzDLfaPkgRmGAxortyQ8vgGP7VJ7Mhk7fzbu5rSugZw8a14UFl328zMG0yudV5faw83mSYDjJhWRfXstBXjskaceTT9ego2Xt/Kxt3ytuIHeh43pmEFd0tY83kl+E31zouGiaE6SQYVDR9tif4o2H3t3+p+vy1xiYgHppz7mji/Fr/qTRSFSHnnmmPJhsarapltqeQQY63/KO7tSGcxzTsR9kwMo+qtCbsKyzAPP8HlxC/x83LW0Qi4c3fJxEhLckt8ypNKoQ379ywHuyqON9Lf41CuI7dQXVhkk3BIwMdWWsqrVWEshnNgiNefUqvFey2vuBtnAgMBAAECggEAWk7UPAxMQ4SxkCI9tCx+rdn8vQe9DwlwUqJr+S5PAQWTDlSi14vUfhfCrGKJur3RZjd04h90xSP/F5TR6AEF44U1KK7KWMVpuW96EJIbp9qLsyqcRluUZxCxy6MwDMTnd+9BTjRuuK8AaiTaz8FOfxGnlMEe3Muj0pZs2wjDLBhPv+8mNux//Rx1hCHxQA+2xXNG2ztNbQ7bSoDHOsz5snCOU3p0QoWH04w4sJ77z+Q6kM8NinXmY7Mu8jW569IXSJdvRrCj4CYyJXMIeXtN3ZQG9gb8zjJmlOBBtxWG6+6U3JuYTRq1DjkyirKKTTFd/FRFA/xM0ulSGHouaYa+cQKBgQDJSw4ATbnrkWgYocXlhCEPVpVecEe5zsYc4XF/RANKZJgoO9TyV+dhm/gz7M98MFLv5PmanxW+gJ+rMoV8Fn9g0Avrcsr37e403FXZ4zZxlWVcZrVY6W64RGdkNWOB47Fc56M43SfJtljeqydB/V60AiGxMElhnjPOXxcGqcxkuwKBgQC9W+5LZL3AJxKXBgS7uwkOpGAnpdVYZ6sWDb/0AB9qi4pkzTPQFoUwIrFpav3mk4CpCP7+SmCr6IwA+YRBphOWnSvF1qRFYEyCnu3HNps1D8iniTA8yEmow7+ekqZBi/QlFsqNhiOLae8Ihd3b93Lwn2yYF+UwbdF1nDRFFo4PRQKBgEMl3GfRheHFQbplzMRBjV22fmFghyy+g2wC8H+Jyqc4Y2GYg/LLzA3Nn8CCkLxWynPoKDOiOfiwo5LSaaD5SEidXjyOsJKANmQ/1Au5NeIx+fwW7wvX4u1pNCxDewxLTlE1nEdNzwYE5CRzZ8clpD/cLi0TdIcx5QpEwkOgxOQdAoGAdKGg/pMIRMhMLPUqCS9Eft5D02fTJmaD/EtvxawlrYxRK5EwrqSQWHul5pZd2J1Vi8fct2e8b2YGhbsp+P1/5y7bfTmdaJXNyk51ilRZ+K7GGlrneSisJeSbq+ILwSegsw98pvrN/dq7vfYmFowtIYQwcOWW5uNdNiGGGF+yFDkCgYB4UVX6RAtMPAL8Y4adLM49Z/H5o/SqrTXIHpWAdUkXI9P5CY2ewilnlW544AjfuhoPoHpWLdOGAOHlInSfizuJ0o5qqWsGUlHF1Z0mvQTd/kxHg84f+Ra7mZ+t0/j844cHdrxn7qU0Q7B+JF7vNwQfq7CJhYREa2PPCCnt4Bz+rQ==";
    public String merchant_private_key;

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
//    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkfJ0URZeK1FHLaoarMRaOgXUbByz2TlC+8w28pv273HYM/3+iPL+OKkiR8lLkWKyyxWW1mInp+1vMPeM1XNNgmFBcnbOHJ8K3iprt6lbBbE6tquenJIT0C/vOqsamFsyLNYQu4O9MRjT9DxttRrL2Zhe4CdpOH070402dkx0ZViKyIf/vWLnGPDzrvPC6D3sdKjeNA8LScju3Bn8G4iimsrAMHogR0qFOhoBGd2yrTF+XMfd3fpL/P+bNyx8w8FoZwFHf4sX4/+JDDsujYx8naGimdhlRkDU0wdM13zEGyII6nqqRJUoS1JzTSbo2n1lQfGcIqJMgtOUqDa8pnjuxQIDAQAB";
    public String alipay_public_key;

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url;

    // 签名方式
    private  String sign_type;

    // 字符编码格式
    private  String charset;

    //订单超时时间
    private String timeout;

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl;

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        log.info("支付宝支付的响应："+result);

        return result;

    }
}
